package com.sjl.mapper.comm;


import cn.hutool.log.StaticLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * <div>
 * 使用此类有几个默认规则： 表名和类名相对应，属性名和字段名对应
 * java类内用驼峰命名法；数据库表和字段都用下划线
 * 类的属性定义的变量名和get/set方法名，以及set的参数类型一致，才会映射到数据库字段
 * 例如： private String name; 而且有 public void setName(String name) 和 public String getName()方法
 * 如果不是严格按照此规则定义的属性，不会被影射到数据库字段上
 * </div>
 *
 * @author sjl
 */
public class InsertUpdateSqlProvider {
    /**
     * id如果传入了值，会被insert使用；如果id为null，不会被insert的columns列出
     */
    public static String insert(Object obj) {
        Map<String, String> map;
        try {
            map = getFieldsMap(obj, true, null);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return new SQL() {
            {
                INSERT_INTO(getTableName(obj));
                for (String col : map.keySet()) {
                    VALUES(col, map.get(col));
                }
            }
        }.toString();
    }

    private static String updateById(Object obj, boolean includeNullValueField, String keyName) {
        Map<String, String> map;
        String keyNameValue;
        String key;
        try {
            map = getFieldsMap(obj, includeNullValueField, keyName);
            key = camelCase2Underscore(keyName);
            keyNameValue = map.get(keyName);
            map.remove(key);
            map.remove(keyName);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return new SQL() {
            {
                UPDATE(getTableName(obj));

                for (String col : map.keySet()) {
                    SET(col + "=" + map.get(col));
                }
                WHERE(key + " = " + keyNameValue );
            }
        }.toString();
    }

    /**
     * 通用更新
     *
     * @param obj     实体
     * @param keyName 主键
     * @return 更新条数
     */
    public static String updateById(Object obj, String keyName) {
        return updateById(obj, true, keyName);
    }

    public static String updateNonNullById(Object obj, String keyName) {
        return updateById(obj, false, keyName);
    }

    private static Map<String, String> getFieldsMap(Object obj, boolean includeNullValue, String keyName)
            throws IllegalArgumentException, IllegalAccessException {
        HashMap<String, String> result = new HashMap<>();
        Class<?> cls = obj.getClass();
        // getDeclaredFields
        Field[] fields = cls.getDeclaredFields();
        for (Field f : fields) {
            String col = f.getName();
            String colName = col.substring(0, 1).toUpperCase() + col.substring(1);
            f.setAccessible(true);
            try {
                cls.getMethod("get" + colName);
                cls.getMethod("set" + colName, f.getType());
                //获取到key 值
                if (StringUtils.equals(keyName, col)) {
                    result.put(keyName, f.get(obj) + "");
                }
            } catch (NoSuchMethodException | SecurityException e) {
                continue;
            }
            if ((!StringUtils.equals(keyName, col) && includeNullValue) || StringUtils.isNotEmpty(f.get(obj)+"") ) {
                result.put(camelCase2Underscore(col), "#{param1." + col + "}");
            }
        }
        return result;
    }

    private static String getTableName(Object obj) {
        return camelCase2Underscore(obj.getClass().getSimpleName());
    }

    private final static String REGEX= "(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])";
    private static String camelCase2Underscore(String s) {
        StringBuilder buf = new StringBuilder();
        for (String w : s.split(REGEX)) {
            buf.append("_").append(w.toLowerCase());
        }
        return buf.substring(1);
    }

    public static void main(String[] args) {
        StaticLog.info(camelCase2Underscore("userId"));
    }

}
